<?php

/**
 * @author Marc André Audet <root@manhim.net>
 * @copyright Copyright(c) 2009-2010 Marc André Audet
 * @license http://www.gnu.org/licenses/gpl.txt The GNU General Public License version 3
 *
 * $Rev: 164 $
 *
 *   GStats++ http://code.google.com/gstatspp/
 *
 *   Copyright (C) 2009-2010 Marc André Audet
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

if (!defined('IN_MANHIM_FRAMEWORK')) exit();

class FModule_GStatsppInstall extends Framework_Modules
{
	private $_Languages = null;
	private $_Language = null;
	private $_Page = null;

	public function __construct()
	{
		$this->SetIsMainModule(true);
	}

	public function Main()
	{
		$_Args = $this->GetArgs();
		$_Config = &$this->Modules->GStatsppConfig;

		$Uri = array();
		if (isset($_Args['QUERY_STRING']))
		{
			$Uri = explode('/', $_Args['QUERY_STRING']);
		}

		if ($this->Modules->GStatsppLanguage->Exists($Uri[0], 'Install'))
		{
			$this->_Page = $Uri[1];
			$this->_Language = $Uri[0];
		}
		else
		{
			$this->_Page = $Uri[0];
			$this->_Language = GSTATSPP_DEFAULTLANGUAGE;
		}

		$this->_Languages = $this->Modules->GStatsppLanguage->GetLanguage($this->_Language, 'Install');

		if ($_Config->GetValue(array('Install', 'IsInstalled')) == true)
		{
			$t = new Templates('Templates/GStatspp/');
			$t->Template('Error.tpl.php')->Assign(array('Message' => 'GStats++ is already installed.'))->Display();
       	}
		else
		{
			switch ($this->GetPage())
			{
				case 'Step1': default:

					$_SESSION['GStatsppInstall'] = array();
					$_SESSION['GStatsppInstall']['Step'] = 1;
					$_SESSION['GStatsppInstall']['AcceptLicense'] = false;
					$_SESSION['GStatsppInstall']['Environment'] = false;
					$_SESSION['GStatsppInstall']['Configured'] = false;

					$t = new Templates('Templates/GStatspp/Default/Install/');
					$t->Template('Step1.tpl.php')->Assign(array('This' => &$this))->Display();

					break;
				case 'Step2':

					if (isset($_POST['submit']) && $_SESSION['GStatsppInstall']['Step'] >= 1)
					{
						$_SESSION['GStatsppInstall']['Step'] = 2;
						$_SESSION['GStatsppInstall']['AcceptLicense'] = true;
						$_SESSION['GStatsppInstall']['Environment'] = false;
						$_SESSION['GStatsppInstall']['Configured'] = false;

						$Tests = array(
							$this->Language('PHP Version') => array(CHECK_ERROR, '5.2.0', '5.1.0', PHP_VERSION),
							$this->Language('PDO Class Loaded') => array(CHECK_ERROR),
							$this->Language('MPDO Class Loaded') => array(CHECK_ERROR),
							$this->Language('Templates Class Loaded') => array(CHECK_ERROR),
							$this->Language('Config.inc.php is writeable') => array(CHECK_ERROR),
						);

						$TestsMessages = array(
							$this->Language('PHP Version') => $this->Language('Unchecked'),
							$this->Language('PDO Class Loaded') => $this->Language('Unchecked'),
							$this->Language('MPDO Class Loaded') => $this->Language('Unchecked'),
							$this->Language('Templates Class Loaded') => $this->Language('Unchecked'),
							$this->Language('Config.inc.php is writeable') => $this->Language('Unchecked'),
						);

						$Continue = true;

						if (version_compare(PHP_VERSION, $Tests[$this->Language('PHP Version')][1]) >= 0)
						{
							$Tests[$this->Language('PHP Version')] = array(CHECK_OK, $Tests[$this->Language('PHP Version')][1], $Tests[$this->Language('PHP Version')][2], PHP_VERSION);
							$TestsMessages[$this->Language('PHP Version')] = null;
						}
						else if (version_compare(PHP_VERSION, $Tests[$this->Language('PHP Version')][2]) >= 0)
						{
							$Tests[$this->Language('PHP Version')] = array(CHECK_WARNING, $Tests[$this->Language('PHP Version')][1], $Tests[$this->Language('PHP Version')][2], PHP_VERSION);
							$TestsMessages[$this->Language('PHP Version')] = sprintf($this->Language('You might consider upgrading your current version of PHP to at least %1$s.'), $Tests[$this->Language('PHP Version')][1]);
						}
						else
						{
							$Tests[$this->Language('PHP Version')] = array(CHECK_ERROR, $Tests[$this->Language('PHP Version')][1], $Tests[$this->Language('PHP Version')][2], PHP_VERSION);
							$TestsMessages[$this->Language('PHP Version')] = sprintf($this->Language('A version of at least %1$s is required for the software to run correctly. Please update your version of PHP.'), $Tests[$this->Language('PHP Version')][2]);
							$Continue = false;
						}

						if (class_exists('MPDO') && class_exists('MPDOQuery'))
						{
							$Tests[$this->Language('MPDO Class Loaded')] = array(CHECK_OK);
							$TestsMessages[$this->Language('MPDO Class Loaded')] = null;
						}
						else
						{
							$Tests[$this->Language('MPDO Class Loaded')] = array(CHECK_ERROR);
							$Tests[$this->Language('MPDO Class Loaded')] = $this->Language('Your installation of GStats++ might have been corrupted. Please re-download the files and re-install.');
							$Continue = false;
						}

						$exts = get_loaded_extensions(false);

						if (class_exists('PDO') && in_array('PDO', $exts) && in_array('pdo_mysql', $exts) && in_array('pdo_sqlite', $exts))
						{
							$Tests[$this->Language('PDO Class Loaded')] = array(CHECK_OK);
							$TestsMessages[$this->Language('PDO Class Loaded')] = null;
						}
						else if (class_exists('PDO') && in_array('PDO', $exts) && in_array('pdo_mysql', $exts))
						{
							$Tests[$this->Language('PDO Class Loaded')] = array(CHECK_WARNING);
							$TestsMessages[$this->Language('PDO Class Loaded')] = $this->Language('The PDO_SQLITE extension is not loaded, therefore it won\'t be able to connect to SQLite databases.');
						}
						else
						{
							$Tests[$this->Language('PDO Class Loaded')] = array(CHECK_ERROR);
							$Tests[$this->Language('PDO Class Loaded')] = $this->Language('PDO is required in order for GStats++ to work. You will need to configure PHP with PDO support with PDO_MySQL and PDO_SQLite drivers.');
							$Continue = false;
						}

						if (class_exists('Templates'))
						{
							$Tests[$this->Language('Templates Class Loaded')] = array(CHECK_OK);
							$TestsMessages[$this->Language('Templates Class Loaded')] = null;
						}
						else
						{
							$Tests[$this->Language('Templates Class Loaded')] = array(CHECK_ERROR);
							$TestsMessages[$this->Language('Templates Class Loaded')] = $this->Language('Your installation of GStats++ might have been corrupted. Please re-download the files and re-install.');
							$Continue = false;
						}

						$Tests[$this->Language('Config.inc.php is writeable')] = array(CHECK_OK);
						$TestsMessages[$this->Language('Config.inc.php is writeable')] = null;

						if (!file_exists(GSTATSPP_CONFIGFILE))
						{
							@chmod(GSTATSPP_CONFIGFOLDER, 0755);
							@touch(GSTATSPP_CONFIGFILE) or $f = @fopen(GSTATSPP_CONFIGFILE, 'w');
							if (isset($f)) @fclose($f);
						}

						if (!file_exists(GSTATSPP_CONFIGFILE))
						{
							@chmod(GSTATSPP_CONFIGFOLDER, 0775);
							@touch(GSTATSPP_CONFIGFILE) or $f = @fopen(GSTATSPP_CONFIGFILE, 'w');
							if (isset($f)) @fclose($f);
						}

						if (!file_exists(GSTATSPP_CONFIGFILE))
						{
							@chmod(GSTATSPP_CONFIGFOLDER, 07777);
							@touch(GSTATSPP_CONFIGFILE) or $f = @fopen(GSTATSPP_CONFIGFILE, 'w');
							if (isset($f)) @fclose($f);
						}

						if (file_exists(GSTATSPP_CONFIGFILE))
						{
							if (!is_readable(GSTATSPP_CONFIGFILE) || !is_writeable(GSTATSPP_CONFIGFILE))
							{
								@chmod(GSTATSPP_CONFIGFILE, 0644);
							}

							if (!is_readable(GSTATSPP_CONFIGFILE) || !is_writeable(GSTATSPP_CONFIGFILE))
							{
								@chmod(GSTATSPP_CONFIGFILE, 0664);
							}

							if (!is_readable(GSTATSPP_CONFIGFILE) || !is_writeable(GSTATSPP_CONFIGFILE))
							{
								@chmod(GSTATSPP_CONFIGFILE, 0666);
							}

							if (!is_readable(GSTATSPP_CONFIGFILE) || !is_writeable(GSTATSPP_CONFIGFILE))
							{
								@chmod(GSTATSPP_CONFIGFILE, 0775);
							}

							if (!is_readable(GSTATSPP_CONFIGFILE) || !is_writeable(GSTATSPP_CONFIGFILE))
							{
								@chmod(GSTATSPP_CONFIGFILE, 0777);
							}

							if (!is_readable(GSTATSPP_CONFIGFILE) || !is_writeable(GSTATSPP_CONFIGFILE))
							{
								$Tests[$this->Language('Config.inc.php is writeable')] = array(CHECK_ERROR);
								$TestsMessages[$this->Language('Config.inc.php is writeable')] = sprintf($this->Language('The file %1$s is not writable. Please change the access rights so this software can write and read from the file %1$s from the %2$s folder.'), 'Config.inc.php', 'Config/');
								$Continue = false;
							}
						}
						else
						{
							$Tests[$this->Language('Config.inc.php is writeable')] = array(CHECK_ERROR);
							$TestsMessages[$this->Language('Config.inc.php is writeable')] = sprintf($this->Language('The setup couldn\'t create the %1$s file, please make an empty %1$s file in the %2$s folder and give it write and read accesses for the software.'), 'Config.inc.php', 'Config/');
							$Continue = false;
						}

						if ($Continue == false)
						{
							$_SESSION['GStatsppInstall']['Environment'] = false;
						}
						else
						{
							$_SESSION['GStatsppInstall']['Environment'] = true;
						}

						$t = new Templates('Templates/GStatspp/Default/Install/');
						$t->Template('Step2.tpl.php')->Assign(array('This' => $this, 'Tests' => $Tests, 'TestsMessages' => $TestsMessages, 'Continue' => $Continue))->Display();
                   	}
					else
					{
						$t = new Templates('Templates/GStatspp/');
						$t->Template('Error.tpl.php')->Assign(array('Message' => 'You need to accept the agreements before accessing this page.'))->Display();
					}

					break;
				case 'Step3':

					if (isset($_POST['submit']) && $_SESSION['GStatsppInstall']['Step'] >= 2
						&& $_SESSION['GStatsppInstall']['Environment'] == true
						&& $_SESSION['GStatsppInstall']['AcceptLicense'] == true)
					{
						$_SESSION['GStatsppInstall']['Step'] = 3;
						$_SESSION['GStatsppInstall']['Configured'] = false;

						$t = new Templates('Templates/GStatspp/Default/Install/');
						$t->Template('Step3.tpl.php')->Assign(array('This' => $this))->Display();
					}
					else
					{
						$t = new Templates('Templates/GStatspp/');
						$t->Template('Error.tpl.php')->Assign(array('Message' => 'You need to accept the agreements and check your environment before accessing this page.'))->Display();
					}

					break;
			}
		}
	}

	public function Language($name)
	{
		return $this->_Languages->Language($name);
	}

	public function GetLanguage()
	{
		return $this->_Language;
	}

	public function GetPage()
	{
		return $this->_Page;
	}
}

?>
